package com.ruoyi.web.core.config;

import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author nine
 * @version 1.0
 * @description 启用 knife4j，注释掉swagger
 * @date 2023/3/3 23:09
 */
@Configuration
@EnableSwagger2
public class Knife4jConfig {

    // 引入Knife4j提供的扩展类
    private final OpenApiExtensionResolver openApiExtensionResolver;

    @Autowired
    public Knife4jConfig(OpenApiExtensionResolver openApiExtensionResolver) {
        this.openApiExtensionResolver = openApiExtensionResolver;
    }

    @Bean
    public Docket adminApiConfig() {
        // 分组名称
        String groupName = "adminApi";
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .host("https://www.nine.com")
                .groupName(groupName)
                .apiInfo(adminApiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.ruoyi"))
                // 显示 admin 路径下的页面
                //.paths(PathSelectors.regex("/admin/.*"))
                // 显示所有
                .paths(PathSelectors.any())
                .build()
                //赋予插件体系
                .extensions(openApiExtensionResolver.buildExtensions(groupName));

        return docket;
    }

    public ApiInfo adminApiInfo() {
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("后台管理系统-API文档")
                .description("相关接口定义信息")
                .version("1.0")
                .contact(new Contact("nine", "https://www.nine.com", "nine@qq.com"))
                .build();
        return apiInfo;
    }


}
